package com.car.carcontrol;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.text.InputFilter;
import android.text.InputType;
import android.text.LoginFilter;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.car.carcontrol.base.BaseActivity;
import com.car.carcontrol.base.BaseApplication;
import com.car.carcontrol.base.BaseViewAdapter;
import com.car.carcontrol.util.IntentUtils;
import com.car.carcontrol.util.SharedPreferencesUrls;
import com.car.carcontrol.util.ToastUtil;
import com.google.gson.Gson;
import com.monkey.miclockview.MiClockView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import cn.jock.pickerview.view.view.OptionsPickerView;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

//添加、更新界面控件
public class AddWidgetActivity extends BaseActivity {

    private FrameLayout v;

    private long loopTime = 60 * 1000;  //时间显示间隔   默认1分钟
    private int cn = 10;     //屏保显示间隔   默认10分钟

    private int total = 0;  //要添加的所有按钮总数


    private int width = BaseApplication.width;
    private int height = BaseApplication.height;
    private float density = BaseApplication.density;

    Handler handler = new Handler();

    //显示区背景控件
    private ImageView iv_add_bg;

    //添加图片控件
    private ImageView iv_add1;
    private ImageView iv_add2;
    private ImageView iv_add3;
    private ImageView iv_add4;
    private ImageView iv_add5;
    private ImageView iv_add6;

    //添加图片发送指令输入框控件
    private EditText et_add1;
    private EditText et_add2;
    private EditText et_add3;
    private EditText et_add4;
    private EditText et_add5;
    private EditText et_add6;

    private MiClockView miClockView;

    int iconW0 = 145;   //图标库图片原始宽度
    int iconH0 = 152;   //图标库图片原始高度
    int iconW = iconW0*width/1280;  //添加图片宽度
    int iconH = iconH0*height/480;  //添加图片高度
    int hlv_iconW = 72*width/1280;  //ListView图片宽度
    int hlv_iconH = 76*height/480;  //ListView图片高度
    int hlv_leftMargin = 14*width/1280;  //ListView图片左边距
    int hlv_rightMargin = 14*width/1280;  //ListView图片右边距
    int hlv_topMargin = 370*height/480; //ListView上边距

    Object[] allParam = new Object[]{
            new Object[]{"iv", new Rect(0*width/1280, 0*height/480, 1280*width/1280, 480*height/480), R.drawable.bg, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            new Object[]{"iv", new Rect(0*width/1280, 0*height/480, (1280-246)*width/1280, (480-140)*height/480), R.drawable.bg1, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},      //添加区背景
            new Object[]{"iv", new Rect(1220*width/1280, 10*height/480, 46*width/1280, 34*height/480), R.drawable.back_image, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},


            new Object[]{"iv", new Rect(100*width/1280, 150*height/480, iconW, iconH), R.drawable.add_icon, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},   //添加按钮
            new Object[]{"iv", new Rect(300*width/1280, 150*height/480, iconW, iconH), R.drawable.add_icon2, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(500*width/1280, 150*height/480, iconW, iconH), R.drawable.add_icon3, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(700*width/1280, 150*height/480, iconW, iconH), R.drawable.add_icon4, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(900*width/1280, 150*height/480, iconW, iconH), R.drawable.add_icon5, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(1100*width/1280, 150*height/480, iconW, iconH), R.drawable.add_icon6, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            new Object[]{"et", new Rect(1050*width/1280, 50*height/480, 0, 0), "+1", 12, Color.BLACK, true, new byte[]{0x00,0x00,0x00}, R.drawable.btn_bcg2},      //界面按钮数选择
            new Object[]{"et", new Rect((1050+85*1)*width/1280, 50*height/480, 0, 0), "+2", 12, Color.BLACK, true, new byte[]{0x00,0x00,0x00}, R.drawable.btn_bcg2},
            new Object[]{"et", new Rect((1050+85*2)*width/1280, 50*height/480, 0, 0), "+3", 12, Color.BLACK, true, new byte[]{0x00,0x00,0x00}, R.drawable.btn_bcg2},
            new Object[]{"et", new Rect(1050*width/1280, 150*height/480, 0, 0), "+4", 12, Color.BLACK, true, new byte[]{0x00,0x00,0x00}, R.drawable.btn_bcg2},
            new Object[]{"et", new Rect((1050+85*1)*width/1280, 150*height/480, 0, 0), "+5", 12, Color.BLACK, true, new byte[]{0x00,0x00,0x00}, R.drawable.btn_bcg2},
            new Object[]{"et", new Rect((1050+85*2)*width/1280, 150*height/480, 0, 0), "+6", 12, Color.BLACK, true, new byte[]{0x00,0x00,0x00}, R.drawable.btn_bcg2},


            new Object[]{"tv", new Rect(1050*width/1280, 50*height/480, 0, 0), "1", 19, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},      //界面按钮数选择
            new Object[]{"tv", new Rect((1050+85*1)*width/1280, 50*height/480, 0, 0), "2", 19, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},
            new Object[]{"tv", new Rect((1050+85*2)*width/1280, 50*height/480, 0, 0), "3", 19, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},
            new Object[]{"tv", new Rect(1050*width/1280, 150*height/480, 0, 0), "4", 19, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},
            new Object[]{"tv", new Rect((1050+85*1)*width/1280, 150*height/480, 0, 0), "5", 19, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},
            new Object[]{"tv", new Rect((1050+85*2)*width/1280, 150*height/480, 0, 0), "6", 19, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},

            new Object[]{"tv", new Rect((1050+85*2)*width/1280, 150*height/480, 0, 0), "6", 19, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},

            new Object[]{"tv", new Rect(1050*width/1280, 250*height/480, 0, 0), "背景设置", 12, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},

            new Object[]{"tv", new Rect(1200*width/1280, 250*height/480, 0, 0), "提交", 12, Color.BLACK, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},

            //图标库ListView
            new Object[]{"hlv", new Rect(0 * width / 1280, hlv_topMargin, FrameLayout.LayoutParams.MATCH_PARENT, 100 * height / 480),
                    new Object[]{   //图标库item参数数组：图片资源id、图片点击后的资源id、点击发送的指令
                            new Object[]{R.drawable.light1, R.drawable.light1f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.light2, R.drawable.light2f, new byte[]{0x02,0x01,0x02}},
                            new Object[]{R.drawable.light3, R.drawable.light3f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.light4, R.drawable.light4f, new byte[]{0x04,0x01,0x02}},
                            new Object[]{R.drawable.light5, R.drawable.light5f, new byte[]{0x05,0x01,0x02}},
                            new Object[]{R.drawable.light6, R.drawable.light6f, new byte[]{0x06,0x01,0x02}},
                            new Object[]{R.drawable.light7, R.drawable.light7f, new byte[]{0x07,0x01,0x02}},
                            new Object[]{R.drawable.light8, R.drawable.light8f, new byte[]{0x08,0x01,0x02}},
                            new Object[]{R.drawable.light9, R.drawable.light9f, new byte[]{0x09,0x01,0x02}},
                            new Object[]{R.drawable.light10, R.drawable.light10f, new byte[]{0x10,0x01,0x02}},
                            new Object[]{R.drawable.light11, R.drawable.light11f, new byte[]{0x11,0x01,0x02}},
                            new Object[]{R.drawable.light12, R.drawable.light12f, new byte[]{0x12,0x01,0x02}},
                            new Object[]{R.drawable.light13, R.drawable.light13f, new byte[]{0x13,0x01,0x02}},
                            new Object[]{R.drawable.light14, R.drawable.light14f, new byte[]{0x14,0x01,0x02}},
                            new Object[]{R.drawable.light15, R.drawable.light15f, new byte[]{0x15,0x01,0x02}},
                            new Object[]{R.drawable.light16, R.drawable.light16f, new byte[]{0x16,0x01,0x02}},
                            new Object[]{R.drawable.light17, R.drawable.light17f, new byte[]{0x17,0x01,0x02}},
                            new Object[]{R.drawable.light18, R.drawable.light18f, new byte[]{0x18,0x01,0x02}},
                            new Object[]{R.drawable.light19, R.drawable.light19f, new byte[]{0x19,0x01,0x02}},
                            new Object[]{R.drawable.light20, R.drawable.light20f, new byte[]{0x20,0x01,0x02}},
                            new Object[]{R.drawable.light21, R.drawable.light21f, new byte[]{0x21,0x01,0x02}},
                            new Object[]{R.drawable.light22, R.drawable.light22f, new byte[]{0x22,0x01,0x02}},
                            new Object[]{R.drawable.light23, R.drawable.light23f, new byte[]{0x23,0x01,0x02}},
                            new Object[]{R.drawable.light24, R.drawable.light24f, new byte[]{0x24,0x01,0x02}},
                            new Object[]{R.drawable.light25, R.drawable.light25f, new byte[]{0x25,0x01,0x02}},
                            new Object[]{R.drawable.light26, R.drawable.light26f, new byte[]{0x26,0x01,0x02}},
                            new Object[]{R.drawable.light27, R.drawable.light27f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.light28, R.drawable.light28f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.light29, R.drawable.light29f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.light30, R.drawable.light30f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.light31, R.drawable.light31f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.light32, R.drawable.light32f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.light33, R.drawable.light33f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.light34, R.drawable.light34f, new byte[]{0x03,0x01,0x02}},
                            new Object[]{R.drawable.device1, R.drawable.device1f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device2, R.drawable.device2f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device3, R.drawable.device3f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device4, R.drawable.device4f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device5, R.drawable.device5f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device6, R.drawable.device6f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device7, R.drawable.device7f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device8, R.drawable.device8f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device9, R.drawable.device9f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device10, R.drawable.device10f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device11, R.drawable.device11f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device12, R.drawable.device12f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device13, R.drawable.device13f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device14, R.drawable.device14f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device15, R.drawable.device15f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device16, R.drawable.device16f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device17, R.drawable.device17f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device18, R.drawable.device18f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device19, R.drawable.device19f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device20, R.drawable.device20f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device21, R.drawable.device21f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device22, R.drawable.device22f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device23, R.drawable.device23f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device24, R.drawable.device24f, new byte[]{0x01,0x01,0x02}},
                            new Object[]{R.drawable.device25, R.drawable.device25f, new byte[]{0x01,0x01,0x02}},

                    },
            },


            new Object[]{"mcv", new Rect(0*width/1280, 0*height/480, 1280*width/1280, 480*height/480), "小米时钟", 19, Color.BLACK, R.drawable.clock_bg, R.drawable.needle_second, R.drawable.needle_minute, R.drawable.needle_hour, R.drawable.open},

    };

    Object[] allParam2 = new Object[]{};    //要修改界面控件参数数组

    private OptionsPickerView pvOptions;
    private ArrayList<String> item = new ArrayList<>();

    //界面名字
    String name;
    String wName1;
    String wName2;
    String wName3;
    String wName4;
    String wName5;
    String wName6;

    //添加界面的图片资源id
    int imgSrc1;
    int imgSrc2;
    int imgSrc3;
    int imgSrc4;
    int imgSrc5;
    int imgSrc6;

    //添加界面的图片点击后资源id
    int imgSrc1f;
    int imgSrc2f;
    int imgSrc3f;
    int imgSrc4f;
    int imgSrc5f;
    int imgSrc6f;

    int btnId = 0;  //表示显示区第几个按钮被点

    int iv_id=0;    //修改界面时表示加载到第几个控件

    //添加界面的图片大小位置
    Rect rect1 = new Rect();
    Rect rect2 = new Rect();
    Rect rect3 = new Rect();
    Rect rect4 = new Rect();
    Rect rect5 = new Rect();
    Rect rect6 = new Rect();

    //添加界面的图片点击发送的串口指令
    int cmdLength = 6;
    byte[] bytes1 = new byte[cmdLength];
    byte[] bytes2 = new byte[cmdLength];
    byte[] bytes3 = new byte[cmdLength];
    byte[] bytes4 = new byte[cmdLength];
    byte[] bytes5 = new byte[cmdLength];
    byte[] bytes6 = new byte[cmdLength];

    HorizontalListView hlv;
    private MyAdapter myAdapter;
    private List<WidgetBean> data = new ArrayList<>();

    int bg = R.drawable.bg1;    //显示区默认背景

    InputFilter[] filters;  //输入法过滤规则

    boolean isModify;   //是否修改界面

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        v = new FrameLayout(this);
        setContentView(v);

        filters = new InputFilter[2];
        filters[0] = new MyInputFilter("0123456789ABCDEF");
        filters[1] = new InputFilter.LengthFilter(6);

        context = this;

        Log.e("awa===onCreate", width+"==="+height+"==="+density);

        name = getIntent().getStringExtra("name");

        String json = SharedPreferencesUrls.getInstance().getString(Config.mapSp.get(name), "");


        if(json==null || "".equals(json)){
            isModify = false;
        }else{
            isModify = true;

            Gson gson = new Gson();
            allParam2 = gson.fromJson(json, Object[].class);

            Log.e("awa===onCreate2", width+"==="+height+"==="+allParam2.length+"==="+allParam2[0]);

        }

        initView();

//        loop();

    }

    //输入法过滤
    public class MyInputFilter extends LoginFilter.UsernameFilterGeneric {
        private String mAllowedDigits;
        private int length;

        public MyInputFilter(String digits) {
            mAllowedDigits = digits;
        }

        @Override
        public boolean isAllowed(char c) {
            if (mAllowedDigits.indexOf(c) != -1) {
                return true;
            }
            return false;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void initView(){

        //背景选择器
        pvOptions = new OptionsPickerView(context,true);
        pvOptions.setTitle("背景类型");

        item.add("背景1");
        item.add("背景2");
        item.add("背景3");
        item.add("背景4");
        item.add("背景5");
        item.add("背景6");
        item.add("背景7");
        item.add("背景8");
        item.add("背景9");
        item.add("背景10");
        item.add("背景11");
        item.add("背景12");
        item.add("背景13");
        pvOptions.setPicker(item);
        pvOptions.setCyclic(false, false, false);
        pvOptions.setSelectOptions(0, 0, 0);

        pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {

                Log.e("sa===onOptionsSelect", options1+"==="+item.size());

                String name = item.get(options1);

                if("背景1".equals(name)){
                    bg = R.drawable.bg1;
                }else if("背景2".equals(name)){
                    bg = R.drawable.bg2;
                }else if("背景3".equals(name)){
                    bg = R.drawable.bg3;
                }else if("背景4".equals(name)){
                    bg = R.drawable.bg4;
                }else if("背景5".equals(name)){
                    bg = R.drawable.bg5;
                }else if("背景6".equals(name)){
                    bg = R.drawable.bg6;
                }else if("背景7".equals(name)){
                    bg = R.drawable.bg7;
                }else if("背景8".equals(name)){
                    bg = R.drawable.bg8;
                }else if("背景9".equals(name)){
                    bg = R.drawable.bg9;
                }else if("背景10".equals(name)){
                    bg = R.drawable.bg10;
                }else if("背景11".equals(name)){
                    bg = R.drawable.bg11;
                }else if("背景12".equals(name)){
                    bg = R.drawable.bg12;
                }else if("背景13".equals(name)){
                    bg = R.drawable.bg13;
                }

                iv_add_bg.setImageResource(bg);

            }
        });

        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                int textBcg = (int)obj[7];
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                tv.setBackgroundResource(textBcg);
                tv.setPadding(20*width/1280, 10*height/480, 20*width/1280, 10*height/480);
                v.addView(tv, params);

                boolean isCanClick = (boolean)obj[5];

                if(isCanClick){
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int textBcg = (int)obj[7];

                            Log.e("sa===tv", name+"==="+textBcg);

                            if(textBcg==R.drawable.btn_bcg){
                                tv.setBackgroundResource(R.drawable.btn_bcg2);
                            }else if(textBcg==R.drawable.btn_bcg2){
                            }

                            tv.setBackgroundResource(R.drawable.btn_bcg);

                            if("1".equals(name) || "2".equals(name) || "3".equals(name) || "4".equals(name) || "5".equals(name) || "6".equals(name)){
                                iv_add1.setVisibility(View.GONE);
                                iv_add2.setVisibility(View.GONE);
                                iv_add3.setVisibility(View.GONE);
                                iv_add4.setVisibility(View.GONE);
                                iv_add5.setVisibility(View.GONE);
                                iv_add6.setVisibility(View.GONE);

                                et_add1.setVisibility(View.GONE);
                                et_add2.setVisibility(View.GONE);
                                et_add3.setVisibility(View.GONE);
                                et_add4.setVisibility(View.GONE);
                                et_add5.setVisibility(View.GONE);
                                et_add6.setVisibility(View.GONE);
                            }

                            //按钮个数选择
                            if("1".equals(name)){
                                iv_add1.setVisibility(View.VISIBLE);
                                et_add1.setVisibility(View.VISIBLE);

                                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(450*width/1280, 100*height/480, iconW, iconH);
                                iv_add1.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(450*width/1280, (100+2)*height/480+iconH, 0, 0);
                                et_add1.setLayoutParams(params);
                                et_add1.setText(bytesToString(bytes1));

                                rect1 = new Rect(450*width/1280, 100*height/480, iconW, iconH);

                                total = 1;
                            }else if("2".equals(name)){
                                iv_add1.setVisibility(View.VISIBLE);
                                iv_add2.setVisibility(View.VISIBLE);
                                et_add1.setVisibility(View.VISIBLE);
                                et_add2.setVisibility(View.VISIBLE);

                                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(275*width/1280, 100*height/480, iconW, iconH);
                                iv_add1.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(625*width/1280, 100*height/480, iconW, iconH);
                                iv_add2.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(275*width/1280, (100+2)*height/480+iconH, 0, 0);
                                et_add1.setLayoutParams(params);
                                et_add1.setText(bytesToString(bytes1));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(625*width/1280, (100+2)*height/480+iconH, 0, 0);
                                et_add2.setLayoutParams(params);
                                et_add2.setText(bytesToString(bytes2));

                                rect1 = new Rect(275*width/1280, 100*height/480, iconW, iconH);
                                rect2 = new Rect(625*width/1280, 100*height/480, iconW, iconH);

                                total = 2;
                            }else if("3".equals(name)){
                                iv_add1.setVisibility(View.VISIBLE);
                                iv_add2.setVisibility(View.VISIBLE);
                                iv_add3.setVisibility(View.VISIBLE);
                                et_add1.setVisibility(View.VISIBLE);
                                et_add2.setVisibility(View.VISIBLE);
                                et_add3.setVisibility(View.VISIBLE);

                                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(100*width/1280, 100*height/480, iconW, iconH);
                                iv_add1.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(450*width/1280, 100*height/480, iconW, iconH);
                                iv_add2.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(800*width/1280, 100*height/480, iconW, iconH);
                                iv_add3.setLayoutParams(params);


                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(100*width/1280, (100+2)*height/480+iconH, 0, 0);
                                et_add1.setLayoutParams(params);
                                et_add1.setText(bytesToString(bytes1));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(450*width/1280, (100+2)*height/480+iconH, 0, 0);
                                et_add2.setLayoutParams(params);
                                et_add2.setText(bytesToString(bytes2));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(800*width/1280, (100+2)*height/480+iconH, 0, 0);
                                et_add3.setLayoutParams(params);
                                et_add3.setText(bytesToString(bytes3));


                                rect1 = new Rect(100*width/1280, 100*height/480, iconW, iconH);
                                rect2 = new Rect(450*width/1280, 100*height/480, iconW, iconH);
                                rect3 = new Rect(800*width/1280, 100*height/480, iconW, iconH);

                                total = 3;
                            }else if("4".equals(name)){
                                iv_add1.setVisibility(View.VISIBLE);
                                iv_add2.setVisibility(View.VISIBLE);
                                iv_add3.setVisibility(View.VISIBLE);
                                iv_add4.setVisibility(View.VISIBLE);
                                et_add1.setVisibility(View.VISIBLE);
                                et_add2.setVisibility(View.VISIBLE);
                                et_add3.setVisibility(View.VISIBLE);
                                et_add4.setVisibility(View.VISIBLE);

                                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(275*width/1280, 10*height/480, iconW, iconH);
                                iv_add1.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(625*width/1280, 10*height/480, iconW, iconH);
                                iv_add2.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(275*width/1280, 180*height/480, iconW, iconH);
                                iv_add3.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(625*width/1280, 180*height/480, iconW, iconH);
                                iv_add4.setLayoutParams(params);


                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(275*width/1280, (10+2)*height/480+iconH, 0, 0);
                                et_add1.setLayoutParams(params);
                                et_add1.setText(bytesToString(bytes1));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(625*width/1280, (10+2)*height/480+iconH, 0, 0);
                                et_add2.setLayoutParams(params);
                                et_add2.setText(bytesToString(bytes2));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(275*width/1280, (180+2)*height/480+iconH, 0, 0);
                                et_add3.setLayoutParams(params);
                                et_add3.setText(bytesToString(bytes3));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(625*width/1280, (180+2)*height/480+iconH, 0, 0);
                                et_add4.setLayoutParams(params);
                                et_add4.setText(bytesToString(bytes4));

                                rect1 = new Rect(275*width/1280, 10*height/480, iconW, iconH);
                                rect2 = new Rect(625*width/1280, 10*height/480, iconW, iconH);
                                rect3 = new Rect(275*width/1280, 180*height/480, iconW, iconH);
                                rect4 = new Rect(625*width/1280, 180*height/480, iconW, iconH);

                                total = 4;
                            }else if("5".equals(name)){
                                iv_add1.setVisibility(View.VISIBLE);
                                iv_add2.setVisibility(View.VISIBLE);
                                iv_add3.setVisibility(View.VISIBLE);
                                iv_add4.setVisibility(View.VISIBLE);
                                iv_add5.setVisibility(View.VISIBLE);
                                et_add1.setVisibility(View.VISIBLE);
                                et_add2.setVisibility(View.VISIBLE);
                                et_add3.setVisibility(View.VISIBLE);
                                et_add4.setVisibility(View.VISIBLE);
                                et_add5.setVisibility(View.VISIBLE);

                                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(275*width/1280, 10*height/480, iconW, iconH);
                                iv_add1.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(635*width/1280, 10*height/480, iconW, iconH);
                                iv_add2.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(100*width/1280, 180*height/480, iconW, iconH);
                                iv_add3.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(450*width/1280, 180*height/480, iconW, iconH);
                                iv_add4.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(800*width/1280, 180*height/480, iconW, iconH);
                                iv_add5.setLayoutParams(params);


                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(275*width/1280, (10+2)*height/480+iconH, 0, 0);
                                et_add1.setLayoutParams(params);
                                et_add1.setText(bytesToString(bytes1));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(625*width/1280, (10+2)*height/480+iconH, 0, 0);
                                et_add2.setLayoutParams(params);
                                et_add2.setText(bytesToString(bytes2));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(100*width/1280, (180+2)*height/480+iconH, 0, 0);
                                et_add3.setLayoutParams(params);
                                et_add3.setText(bytesToString(bytes3));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(450*width/1280, (180+2)*height/480+iconH, 0, 0);
                                et_add4.setLayoutParams(params);
                                et_add4.setText(bytesToString(bytes4));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(800*width/1280, (180+2)*height/480+iconH, 0, 0);
                                et_add5.setLayoutParams(params);
                                et_add5.setText(bytesToString(bytes5));

                                rect1 = new Rect(275*width/1280, 10*height/480, iconW, iconH);
                                rect2 = new Rect(625*width/1280, 10*height/480, iconW, iconH);
                                rect3 = new Rect(100*width/1280, 180*height/480, iconW, iconH);
                                rect4 = new Rect(450*width/1280, 180*height/480, iconW, iconH);
                                rect5 = new Rect(800*width/1280, 180*height/480, iconW, iconH);

                                total = 5;
                            }else if("6".equals(name)){
                                iv_add1.setVisibility(View.VISIBLE);
                                iv_add2.setVisibility(View.VISIBLE);
                                iv_add3.setVisibility(View.VISIBLE);
                                iv_add4.setVisibility(View.VISIBLE);
                                iv_add5.setVisibility(View.VISIBLE);
                                iv_add6.setVisibility(View.VISIBLE);
                                et_add1.setVisibility(View.VISIBLE);
                                et_add2.setVisibility(View.VISIBLE);
                                et_add3.setVisibility(View.VISIBLE);
                                et_add4.setVisibility(View.VISIBLE);
                                et_add5.setVisibility(View.VISIBLE);
                                et_add6.setVisibility(View.VISIBLE);

                                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(100*width/1280, 10*height/480, iconW, iconH);
                                iv_add1.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(450*width/1280, 10*height/480, iconW, iconH);
                                iv_add2.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(800*width/1280, 10*height/480, iconW, iconH);
                                iv_add3.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(100*width/1280, 180*height/480, iconW, iconH);
                                iv_add4.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(450*width/1280, 180*height/480, iconW, iconH);
                                iv_add5.setLayoutParams(params);

                                params = new FrameLayout.LayoutParams(iconW, iconH);
                                params.setMargins(800*width/1280, 180*height/480, iconW, iconH);
                                iv_add6.setLayoutParams(params);


                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(100*width/1280, (10+2)*height/480+iconH, 0, 0);
                                et_add1.setLayoutParams(params);
                                et_add1.setText(bytesToString(bytes1));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(450*width/1280, (10+2)*height/480+iconH, 0, 0);
                                et_add2.setLayoutParams(params);
                                et_add2.setText(bytesToString(bytes2));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(800*width/1280, (10+2)*height/480+iconH, 0, 0);
                                et_add3.setLayoutParams(params);
                                et_add3.setText(bytesToString(bytes3));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(100*width/1280, (180+2)*height/480+iconH, 0, 0);
                                et_add4.setLayoutParams(params);
                                et_add4.setText(bytesToString(bytes4));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(450*width/1280, (180+2)*height/480+iconH, 0, 0);
                                et_add5.setLayoutParams(params);
                                et_add5.setText(bytesToString(bytes5));

                                params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                params.setMargins(800*width/1280, (180+2)*height/480+iconH, 0, 0);
                                et_add6.setLayoutParams(params);
                                et_add6.setText(bytesToString(bytes6));

                                rect1 = new Rect(100*width/1280, 10*height/480, iconW, iconH);
                                rect2 = new Rect(450*width/1280, 10*height/480, iconW, iconH);
                                rect3 = new Rect(800*width/1280, 10*height/480, iconW, iconH);
                                rect4 = new Rect(100*width/1280, 180*height/480, iconW, iconH);
                                rect5 = new Rect(450*width/1280, 180*height/480, iconW, iconH);
                                rect6 = new Rect(800*width/1280, 180*height/480, iconW, iconH);

                                total = 6;
                            }else if("背景设置".equals(name)){
                                pvOptions.show();   //显示背景选择器
                            }else if("提交".equals(name)){

                                Log.e("sa===tv提交", total+"==="+imgSrc1+"==="+imgSrc2);

                                //提交前判断是否有图标未添加
                                if(total==0){
                                    ToastUtil.showMessageApp(context, "请先点1-6按钮");
                                    return;
                                }else if(total==1){
                                    if(imgSrc1==0){
                                        ToastUtil.showMessageApp(context, "还有按钮未添加");
                                        return;
                                    }
                                }else if(total==2){
                                    if(imgSrc1==0 || imgSrc2==0){
                                        ToastUtil.showMessageApp(context, "还有按钮未添加");
                                        return;
                                    }
                                }else if(total==3){
                                    if(imgSrc1==0 || imgSrc2==0 || imgSrc3==0){
                                        ToastUtil.showMessageApp(context, "还有按钮未添加");
                                        return;
                                    }
                                }else if(total==4){
                                    if(imgSrc1==0 || imgSrc2==0 || imgSrc3==0 || imgSrc4==0){
                                        ToastUtil.showMessageApp(context, "还有按钮未添加");
                                        return;
                                    }
                                }else if(total==5){
                                    if(imgSrc1==0 || imgSrc2==0 || imgSrc3==0 || imgSrc4==0 || imgSrc5==0){
                                        ToastUtil.showMessageApp(context, "还有按钮未添加");
                                        return;
                                    }
                                }else if(total==6){
                                    if(imgSrc1==0 || imgSrc2==0 || imgSrc3==0 || imgSrc4==0 || imgSrc5==0 || imgSrc6==0){
                                        ToastUtil.showMessageApp(context, "还有按钮未添加");
                                        return;
                                    }
                                }

                                showDialog();
                            }

//                          write(bytes); //TODO

                        }
                    });
                }

            }else if("et".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                byte[] bytes = (byte[])obj[6];
                int textBcg = (int)obj[7];
                EditText et = new EditText(context);
                et.setText(name);
                et.setTextSize(textSize);
                et.setTextColor(textColor);
                et.setBackgroundResource(textBcg);
                et.setPadding(10*width/1280, 0*height/480, 10*width/1280, 0*height/480);
                et.setFilters(filters);

                v.addView(et, params);

                //串口指令输入框控件、参数全局化
                if("+1".equals(name)){
                    et_add1 = et;
                    bytes1 = bytes;
                }else if("+2".equals(name)){
                    et_add2 = et;
                    bytes2 = bytes;
                }else if("+3".equals(name)){
                    et_add3 = et;
                    bytes3 = bytes;
                }else if("+4".equals(name)){
                    et_add4 = et;
                    bytes4 = bytes;
                }else if("+5".equals(name)){
                    et_add5 = et;
                    bytes5 = bytes;
                }else if("+6".equals(name)){
                    et_add6 = et;
                    bytes6 = bytes;
                }

                et.setVisibility(View.GONE);

                boolean isCanClick = (boolean)obj[5];
            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                if(imgSrc==R.drawable.bg1){
                    iv_add_bg = iv;
                }else if(imgSrc==R.drawable.add_icon){
                    iv_add1 = iv;
                    iv_add1.setVisibility(View.GONE);
                }else if(imgSrc==R.drawable.add_icon2){
                    iv_add2 = iv;
                    iv_add2.setVisibility(View.GONE);
                }else if(imgSrc==R.drawable.add_icon3){
                    iv_add3 = iv;
                    iv_add3.setVisibility(View.GONE);
                }else if(imgSrc==R.drawable.add_icon4){
                    iv_add4 = iv;
                    iv_add4.setVisibility(View.GONE);
                }else if(imgSrc==R.drawable.add_icon5){
                    iv_add5 = iv;
                    iv_add5.setVisibility(View.GONE);
                }else if(imgSrc==R.drawable.add_icon6){
                    iv_add6 = iv;
                    iv_add6.setVisibility(View.GONE);
                }

                boolean isCanClick = (boolean)obj[3];

                if(isCanClick){

                    //先点击+图标高亮，再去图标库横向滚动条选取图标，+图标变成所选图标
                    iv.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            int imgId = (int)obj[2];
                            byte[] bytes = (byte[])obj[4];

                            Log.e("fragment_First===iv", imgId+"===");

                            if(btnId==1 && imgSrc1==0){
                                iv_add1.setImageResource(R.drawable.add_icon);
                            }else if(btnId==2 && imgSrc2==0){
                                iv_add2.setImageResource(R.drawable.add_icon2);
                            }else if(btnId==3 && imgSrc3==0){
                                iv_add3.setImageResource(R.drawable.add_icon3);
                            }else if(btnId==4 && imgSrc4==0){
                                iv_add4.setImageResource(R.drawable.add_icon4);
                            }else if(btnId==5 && imgSrc5==0){
                                iv_add5.setImageResource(R.drawable.add_icon5);
                            }else if(btnId==6 && imgSrc6==0){
                                iv_add6.setImageResource(R.drawable.add_icon6);
                            }

                            if(imgId==R.drawable.back_image){
                                finish();
                            }else if(imgId==R.drawable.add_icon && imgSrc1==0){
                                iv.setImageResource(R.drawable.add_iconf);
                                btnId = 1;
                            }else if(imgId==R.drawable.add_iconf && imgSrc1==0){
                                iv.setImageResource(R.drawable.add_icon);
                                btnId = 0;
                            }else if(imgId==R.drawable.add_icon2 && imgSrc2==0){
                                iv.setImageResource(R.drawable.add_icon2f);
                                btnId = 2;
                            }else if(imgId==R.drawable.add_icon2f && imgSrc2==0){
                                iv.setImageResource(R.drawable.add_icon2);
                                btnId = 0;
                            }else if(imgId==R.drawable.add_icon3 && imgSrc3==0){
                                iv.setImageResource(R.drawable.add_icon3f);
                                btnId = 3;
                            }else if(imgId==R.drawable.add_icon3f && imgSrc3==0){
                                iv.setImageResource(R.drawable.add_icon3);
                                btnId = 0;
                            }else if(imgId==R.drawable.add_icon4 && imgSrc4==0){
                                iv.setImageResource(R.drawable.add_icon4f);
                                btnId = 4;
                            }else if(imgId==R.drawable.add_icon4f && imgSrc4==0){
                                iv.setImageResource(R.drawable.add_icon4);
                                btnId = 0;
                            }else if(imgId==R.drawable.add_icon5 && imgSrc5==0){
                                iv.setImageResource(R.drawable.add_icon5f);
                                btnId = 5;
                            }else if(imgId==R.drawable.add_icon5f && imgSrc5==0){
                                iv.setImageResource(R.drawable.add_icon5);
                                btnId = 0;
                            }else if(imgId==R.drawable.add_icon6 && imgSrc6==0){
                                iv.setImageResource(R.drawable.add_icon6f);
                                btnId = 6;
                            }else if(imgId==R.drawable.add_icon6f && imgSrc6==0){
                                iv.setImageResource(R.drawable.add_icon6);
                                btnId = 0;
                            }else{
                                btnId = 0;
                            }


                            Log.e("fragment_First===iv1", imgId+"==="+btnId);

//                          write(bytes); //TODO

                        }
                    });
                }

            }else if("hlv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                Object[] objects = (Object[])obj[2];

                for (int j = 0; j < objects.length; j++) {      //ListView加载图标库
                    Object[] object = (Object[])objects[j];
                    int imgSrc = (int)object[0];
                    int imgSrc2 = (int)object[1];
                    byte[] bytes = (byte[])object[2];

                    WidgetBean bean = new WidgetBean();
                    bean.setImgSrc(imgSrc);
                    bean.setImgSrc2(imgSrc2);
                    bean.setBytes(bytes);
                    data.add(bean);
                }

                hlv = new HorizontalListView(context);
                myAdapter = new MyAdapter(context);
                myAdapter.setDatas(data);
                hlv.setAdapter(myAdapter);

                v.addView(hlv, params);


                hlv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                        WidgetBean bean = myAdapter.getDatas().get(position);

                        Log.e("awa===onItemLongClick", position+"==="+bean.getName());

                        return false;
                    }
                });


                hlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        WidgetBean bean = myAdapter.getDatas().get(position);

                        Log.e("awa===onItemClick", view+"==="+position+"==="+bean.getName());

                        byte[] bytes = bean.getBytes();

                        //点击图标，获取图标各种属性并全局化，用于其他逻辑
                        if(btnId==1){
                            wName1 = bean.getName();
                            imgSrc1 = bean.getImgSrc();
                            imgSrc1f = bean.getImgSrc2();
                            bytes1 = bytes;
                        }else if(btnId==2){
                            wName2 = bean.getName();
                            imgSrc2 = bean.getImgSrc();
                            imgSrc2f = bean.getImgSrc2();
                            bytes2 = bytes;
                        }else if(btnId==3){
                            wName3 = bean.getName();
                            imgSrc3 = bean.getImgSrc();
                            imgSrc3f = bean.getImgSrc2();
                            bytes3 = bytes;
                        }else if(btnId==4){
                            wName4 = bean.getName();
                            imgSrc4 = bean.getImgSrc();
                            imgSrc4f = bean.getImgSrc2();
                            bytes4 = bytes;
                        }else if(btnId==5){
                            wName5 = bean.getName();
                            imgSrc5 = bean.getImgSrc();
                            imgSrc5f = bean.getImgSrc2();
                            bytes5 = bytes;
                        }else if(btnId==6){
                            wName6 = bean.getName();
                            imgSrc6 = bean.getImgSrc();
                            imgSrc6f = bean.getImgSrc2();
                            bytes6 = bytes;
                        }

                        //+图标显示所选图标，串口参数输入框设初值
                        if(btnId==1){
                            iv_add1.setImageResource(bean.getImgSrc());
                            et_add1.setText(bytesToString(bytes1));
                        }else if(btnId==2){
                            iv_add2.setImageResource(bean.getImgSrc());
                            et_add2.setText(bytesToString(bytes2));
                        }else if(btnId==3){
                            iv_add3.setImageResource(bean.getImgSrc());
                            et_add3.setText(bytesToString(bytes3));
                        }else if(btnId==4){
                            iv_add4.setImageResource(bean.getImgSrc());
                            et_add4.setText(bytesToString(bytes4));
                        }else if(btnId==5){
                            iv_add5.setImageResource(bean.getImgSrc());
                            et_add5.setText(bytesToString(bytes5));
                        }else if(btnId==6){
                            iv_add6.setImageResource(bean.getImgSrc());
                            et_add6.setText(bytesToString(bytes6));
                        }

//                        write(bytes); //TODO

                    }
                });

            }else if("mcv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);


                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];

                miClockView = new MiClockView(context, (int)obj[5], (int)obj[6], (int)obj[7], (int)obj[8], (int)obj[9]);

                miClockView.setBackgroundColor(textColor);

                v.addView(miClockView, params);

                miClockView.setVisibility(View.GONE);

                miClockView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        Log.e("mcv===onTouch", "===");

                        miClockView.setVisibility(View.GONE);
                        return false;
                    }
                });

            }
        }

        for (int i=0; i<allParam2.length; i++){
            Gson gson = new Gson();
            Object[] obj = gson.fromJson((String)allParam2[i], Object[].class);

            Log.e("awa===obj", obj.length+"==="+obj);

            Rect rect = gson.fromJson((String)obj[1], Rect.class);

            String type = (String)obj[0];

            Log.e("awa===obj1", type+"==="+rect);

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = ((Double)obj[3]).intValue();
                int textColor = ((Double)obj[4]).intValue();
                int textBcg = ((Double)obj[7]).intValue();
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                tv.setBackgroundResource(textBcg);
                tv.setPadding(20*width/1280, 10*height/480, 20*width/1280, 10*height/480);
                v.addView(tv, params);

                boolean isCanClick = (boolean)obj[5];

                if(isCanClick){
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("onClick===", v+"===");

                            String name = (String)obj[2];
//                            byte[] bytes = (byte[])obj[4];

//                            write(bytes); //TODO
                        }
                    });

                    tv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {


                            return false;
                        }
                    });
                }
            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = ((Double)obj[2]).intValue();
                int imgSrc_2 = ((Double)obj[3]).intValue();
                boolean isCanClick = (boolean)obj[4];
                byte[] bytes = gson.fromJson((String)obj[5], byte[].class);

//                ImageView iv = new ImageView(context); iv.setImageResource(imgSrc);
//                iv.setScaleType(ImageView.ScaleType.FIT_XY);
//                v.addView(iv, params);


                //显示要修改界面的控件
                if(iv_id==0){
                    iv_add_bg.setImageResource(imgSrc);
                }else if(iv_id==1){
                    iv_add1.setVisibility(View.VISIBLE);
                    et_add1.setVisibility(View.VISIBLE);
                    iv_add1.setImageResource(imgSrc);
                    iv_add1.setLayoutParams(params);

                    imgSrc1 = imgSrc;
                    imgSrc1f = imgSrc_2;
                    bytes1 = bytes;

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(450*width/1280, (100+2)*height/480+iconH, 0, 0);
                    et_add1.setLayoutParams(params);
                    et_add1.setText(bytesToString(bytes1));

                    rect1 = new Rect(450*width/1280, 100*height/480, iconW, iconH);

                }else if(iv_id==2){
                    iv_add2.setVisibility(View.VISIBLE);
                    et_add2.setVisibility(View.VISIBLE);
                    iv_add2.setImageResource(imgSrc);
                    iv_add2.setLayoutParams(params);

                    imgSrc2 = imgSrc;
                    imgSrc2f = imgSrc_2;
                    bytes2 = bytes;

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(275*width/1280, (100+2)*height/480+iconH, 0, 0);
                    et_add1.setLayoutParams(params);
                    et_add1.setText(bytesToString(bytes1));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(625*width/1280, (100+2)*height/480+iconH, 0, 0);
                    et_add2.setLayoutParams(params);
                    et_add2.setText(bytesToString(bytes2));

                    rect1 = new Rect(275*width/1280, 100*height/480, iconW, iconH);
                    rect2 = new Rect(625*width/1280, 100*height/480, iconW, iconH);


                }else if(iv_id==3){
                    iv_add3.setVisibility(View.VISIBLE);
                    et_add3.setVisibility(View.VISIBLE);
                    iv_add3.setImageResource(imgSrc);
                    iv_add3.setLayoutParams(params);

                    imgSrc3 = imgSrc;
                    imgSrc3f = imgSrc_2;
                    bytes3 = bytes;

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(100*width/1280, (100+2)*height/480+iconH, 0, 0);
                    et_add1.setLayoutParams(params);
                    et_add1.setText(bytesToString(bytes1));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(450*width/1280, (100+2)*height/480+iconH, 0, 0);
                    et_add2.setLayoutParams(params);
                    et_add2.setText(bytesToString(bytes2));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(800*width/1280, (100+2)*height/480+iconH, 0, 0);
                    et_add3.setLayoutParams(params);
                    et_add3.setText(bytesToString(bytes3));

                    rect1 = new Rect(100*width/1280, 100*height/480, iconW, iconH);
                    rect2 = new Rect(450*width/1280, 100*height/480, iconW, iconH);
                    rect3 = new Rect(800*width/1280, 100*height/480, iconW, iconH);

                }else if(iv_id==4){
                    iv_add4.setVisibility(View.VISIBLE);
                    et_add4.setVisibility(View.VISIBLE);
                    iv_add4.setImageResource(imgSrc);
                    iv_add4.setLayoutParams(params);

                    imgSrc4 = imgSrc;
                    imgSrc4f = imgSrc_2;
                    bytes4 = bytes;

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(275*width/1280, (10+2)*height/480+iconH, 0, 0);
                    et_add1.setLayoutParams(params);
                    et_add1.setText(bytesToString(bytes1));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(625*width/1280, (10+2)*height/480+iconH, 0, 0);
                    et_add2.setLayoutParams(params);
                    et_add2.setText(bytesToString(bytes2));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(275*width/1280, (180+2)*height/480+iconH, 0, 0);
                    et_add3.setLayoutParams(params);
                    et_add3.setText(bytesToString(bytes3));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(625*width/1280, (180+2)*height/480+iconH, 0, 0);
                    et_add4.setLayoutParams(params);
                    et_add4.setText(bytesToString(bytes4));

                    rect1 = new Rect(275*width/1280, 10*height/480, iconW, iconH);
                    rect2 = new Rect(625*width/1280, 10*height/480, iconW, iconH);
                    rect3 = new Rect(275*width/1280, 180*height/480, iconW, iconH);
                    rect4 = new Rect(625*width/1280, 180*height/480, iconW, iconH);

                }else if(iv_id==5){
                    iv_add5.setVisibility(View.VISIBLE);
                    et_add5.setVisibility(View.VISIBLE);
                    iv_add5.setImageResource(imgSrc);
                    iv_add5.setLayoutParams(params);

                    imgSrc5 = imgSrc;
                    imgSrc5f = imgSrc_2;
                    bytes5 = bytes;

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(275*width/1280, (10+2)*height/480+iconH, 0, 0);
                    et_add1.setLayoutParams(params);
                    et_add1.setText(bytesToString(bytes1));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(625*width/1280, (10+2)*height/480+iconH, 0, 0);
                    et_add2.setLayoutParams(params);
                    et_add2.setText(bytesToString(bytes2));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(100*width/1280, (180+2)*height/480+iconH, 0, 0);
                    et_add3.setLayoutParams(params);
                    et_add3.setText(bytesToString(bytes3));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(450*width/1280, (180+2)*height/480+iconH, 0, 0);
                    et_add4.setLayoutParams(params);
                    et_add4.setText(bytesToString(bytes4));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(800*width/1280, (180+2)*height/480+iconH, 0, 0);
                    et_add5.setLayoutParams(params);
                    et_add5.setText(bytesToString(bytes5));

                    rect1 = new Rect(275*width/1280, 10*height/480, iconW, iconH);
                    rect2 = new Rect(625*width/1280, 10*height/480, iconW, iconH);
                    rect3 = new Rect(100*width/1280, 180*height/480, iconW, iconH);
                    rect4 = new Rect(450*width/1280, 180*height/480, iconW, iconH);
                    rect5 = new Rect(800*width/1280, 180*height/480, iconW, iconH);

                }else if(iv_id==6){
                    iv_add6.setVisibility(View.VISIBLE);
                    et_add6.setVisibility(View.VISIBLE);
                    iv_add6.setImageResource(imgSrc);
                    iv_add6.setLayoutParams(params);

                    imgSrc6 = imgSrc;
                    imgSrc6f = imgSrc_2;
                    bytes6 = bytes;

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(100*width/1280, (10+2)*height/480+iconH, 0, 0);
                    et_add1.setLayoutParams(params);
                    et_add1.setText(bytesToString(bytes1));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(450*width/1280, (10+2)*height/480+iconH, 0, 0);
                    et_add2.setLayoutParams(params);
                    et_add2.setText(bytesToString(bytes2));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(800*width/1280, (10+2)*height/480+iconH, 0, 0);
                    et_add3.setLayoutParams(params);
                    et_add3.setText(bytesToString(bytes3));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(100*width/1280, (180+2)*height/480+iconH, 0, 0);
                    et_add4.setLayoutParams(params);
                    et_add4.setText(bytesToString(bytes4));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(450*width/1280, (180+2)*height/480+iconH, 0, 0);
                    et_add5.setLayoutParams(params);
                    et_add5.setText(bytesToString(bytes5));

                    params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(800*width/1280, (180+2)*height/480+iconH, 0, 0);
                    et_add6.setLayoutParams(params);
                    et_add6.setText(bytesToString(bytes6));

                    rect1 = new Rect(100*width/1280, 10*height/480, iconW, iconH);
                    rect2 = new Rect(450*width/1280, 10*height/480, iconW, iconH);
                    rect3 = new Rect(800*width/1280, 10*height/480, iconW, iconH);
                    rect4 = new Rect(100*width/1280, 180*height/480, iconW, iconH);
                    rect5 = new Rect(450*width/1280, 180*height/480, iconW, iconH);
                    rect6 = new Rect(800*width/1280, 180*height/480, iconW, iconH);

                }

                iv_id++;
                total = iv_id-1;    //计算按钮总数


            }
        }
    }

    //显示对话框：是否添加/修改界面
    void showDialog(){
        CustomDialog.Builder customBuilder = new CustomDialog.Builder(context);
        customBuilder.setTitle("温馨提示").setMessage("是否添加?")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();

                Log.e("sa===showDialog", "===");

                addFrag();
            }
        });
        customBuilder.create().show();
    }

    //16进制字符串转byte[]
    public static byte[] hexStringToByteArray(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        Log.e("StringToByte===1", bytes+"==="+bytes[0]);

        return bytes;
    }

    //byte[]转16进制字符串
    public String bytesToString(byte[] bytes) {
        final char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[bytes.length * 2];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            hexChars[i * 2] = hexArray[v >>> 4];
            hexChars[i * 2 + 1] = hexArray[v & 0x0F];

            sb.append(hexChars[i * 2]);
            sb.append(hexChars[i * 2 + 1]);
            sb.append(' ');
        }
        return sb.toString();
    }

    //添加/修改界面
    private void addFrag(){
        Log.e("awa===addFrag", name+"===");

        Gson gson = new Gson();

        //如果是添加要改首页导航栏item名字数组
        if(!isModify){
            List<String> list = Arrays.asList(MainActivity.names);
            ArrayList<String> arrayList = new ArrayList<String>(list);
            arrayList.add(name);
            MainActivity.names = arrayList.toArray(new String[0]);
            SharedPreferencesUrls.getInstance().putObject("names", arrayList.toArray(new String[0]));
            Config.mapAll.put("names", arrayList.toArray(new String[0]));
        }



        String rect = gson.toJson(new Rect(0, 0, width-width_d, height-height_d));
        String bytes = gson.toJson(hexStringToByteArray(et_add1.getText().toString()));
        String obj0 = gson.toJson(new Object[]{"iv", rect, bg, 0, false, bytes});   //显示区背景

        rect = gson.toJson(rect1);
        bytes = gson.toJson(hexStringToByteArray(et_add1.getText().toString()));
        String obj1 = gson.toJson(new Object[]{"iv", rect, imgSrc1, imgSrc1f, true, bytes});    //添加图片属性分别为：控件类型、控件大小位置、图片资源id、图片点击后资源id、是否能点击、点击后发的指令

        rect = gson.toJson(rect2);
        bytes = gson.toJson(hexStringToByteArray(et_add2.getText().toString()));
        String obj2 = gson.toJson(new Object[]{"iv", rect, imgSrc2, imgSrc2f, true, bytes});

        rect = gson.toJson(rect3);
        bytes = gson.toJson(hexStringToByteArray(et_add3.getText().toString()));
        String obj3 = gson.toJson(new Object[]{"iv", rect, imgSrc3, imgSrc3f, true, bytes});

        rect = gson.toJson(rect4);
        bytes = gson.toJson(hexStringToByteArray(et_add4.getText().toString()));
        String obj4 = gson.toJson(new Object[]{"iv", rect, imgSrc4, imgSrc4f, true, bytes});

        rect = gson.toJson(rect5);
        bytes = gson.toJson(hexStringToByteArray(et_add5.getText().toString()));
        String obj5 = gson.toJson(new Object[]{"iv", rect, imgSrc5, imgSrc5f, true, bytes});

        rect = gson.toJson(rect6);
        bytes = gson.toJson(hexStringToByteArray(et_add6.getText().toString()));
        String obj6 = gson.toJson(new Object[]{"iv", rect, imgSrc6, imgSrc6f, true, bytes});

        //要添加界面所有控件的参数数组:背景、按钮1、按钮2、按钮3、按钮4、按钮5、按钮6
        String[] objs = new String[]{obj0, obj1, obj2, obj3, obj4, obj5, obj6};

        Object[] allParam = new Object[total+1];

        //控件总数=所有按钮数+1个背景
        for (int i = 0; i < total+1; i++) {
            allParam[i] = objs[i];
        }

        Log.e("awa===addFrag", gson.toJson(allParam)+"===");

//        try {
////            String s = compress("[\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":340,\\\\\\\"left\\\\\\\":0,\\\\\\\"right\\\\\\\":984,\\\\\\\"top\\\\\\\":0}\\\",2131165295,0,false,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165455,2131165456,true,\\\"[9,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":10}\\\",2131165390,2131165391,true,\\\"[16,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":95,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165447,2131165448,true,\\\"[5,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":427,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165449,2131165450,true,\\\"[6,1,2]\\\"]\",\"[\\\"iv\\\",\\\"{\\\\\\\"bottom\\\\\\\":152,\\\\\\\"left\\\\\\\":760,\\\\\\\"right\\\\\\\":137,\\\\\\\"top\\\\\\\":180}\\\",2131165392,2131165393,true,\\\"[17,1,2]\\\"]\"]");
//            String s = compress(gson.toJson(allParam));
//
//            Log.e("ma===onCreate", width+"==="+height+"==="+density+"==="+s+"==="+uncompress(s));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        //因为手机内部存储器不能直接存储数组，把要添加界面所有控件的参数数组转为json字符串，然后保存在存储器名字，Config.mapSp.get(name):根据界面名字获取存储器名字，
        SharedPreferencesUrls.getInstance().putString(Config.mapSp.get(name), gson.toJson(allParam));
        Config.mapAll.put(Config.mapSp.get(name), gson.toJson(allParam));

        Log.e("awa===addFrag2", "==="+gson.toJson(Config.mapAll));

        //跳回设置界面
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }


    // 压缩
    public static String compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
        return out.toString("ISO-8859-1");
    }

    // 解压缩
    public static String uncompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        // toString()使用平台默认编码，也可以显式的指定如toString("GBK")
        return out.toString();
    }

    private void loop(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                long n=0;

                while (true){

//                    m_myHandler.sendEmptyMessage(1);

                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    n++;

                    if(n%cn==0){
                        m_myHandler.sendEmptyMessage(2);
                    }

                }

            }
        }).start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
                    break;

                case 2:
                    break;

                default:
                    break;
            }
            return false;
        }
    });


    private class MyAdapter extends BaseViewAdapter<WidgetBean> {

        public MyAdapter(Context context){
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout layout = new LinearLayout(context);

            WidgetBean bean = getDatas().get(position);
            int imgSrc = bean.getImgSrc();
            int imgSrcf = bean.getImgSrc2();
            byte[] bytes = bean.getBytes();

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(hlv_iconW, hlv_iconH);
            params.leftMargin=hlv_leftMargin;
            params.rightMargin=hlv_rightMargin;
            params.gravity = Gravity.CENTER;
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imgSrc);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(params);
            layout.addView(imageView);

            ImageView iv = new ImageView(context);
            iv.setImageResource(imgSrc);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            addContentView(iv, new FrameLayout.LayoutParams(0,0));
            iv.setVisibility(View.GONE);

            Log.e("awa===getView", hlv.getScrollY()+"==="+hlv.getScrollX()+"==="+hlv.getLeft()+"==="+hlv.getFirstVisiblePosition()+"==="+hlv.getLastVisiblePosition()+"==="+hlv.getChildCount()+"==="+bean.getName()+"==="+bean.getBytes());

            //图标库item拖拽事件，可拖拽某个图标到显示区，已拖拽的能被覆盖更新，并区分点击和拖拽
            imageView.setOnTouchListener(new View.OnTouchListener() {
                float x0;
                float y0;
                float dx;
                float dy;

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(event.getAction()==0){
                        dx=0;
                        dy=0;
                        x0 = event.getX();
                        y0 = event.getY();
                    }else if(event.getAction()==2){
                        dx = event.getX()-x0;
                        dy = event.getY()-y0;
                    }

                    if(dx<0.01 && dx>-0.01 && dy<0.01 && dy>-0.01)  return true;    //区分点击和拖动图标

                    iv.setVisibility(View.VISIBLE);

                    int x = hlv_leftMargin + (hlv_leftMargin + hlv_rightMargin + hlv_iconW)*position+(int)dx*width/1280-hlv.mCurrentX;
                    int y = hlv_topMargin + (int)dy*height/480;

                    Log.e("iv===onTouch", hlv.mScroller.getCurrX()+"==="+hlv.mCurrentX+"==="+hlv.getScrollY()+"==="+hlv.getScrollX()+"==="+hlv.getLeft()+"==="+hlv.getFirstVisiblePosition()+"==="+hlv.getLastVisiblePosition()+"==="+hlv.getChildCount()+"==="+event.getAction()+"==="+x0+"==="+dx+"==="+event.getX()+"==="+event.getY()+"==="+total+"==="+x+"==="+y+"==="+width+"==="+height+"==="+500*width/1280+"==="+(x>=500*width/1280 && x<=(500+64)*width/1280 && y>=120*height/480 && y<=(120+64)*height/480));

                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(iconW, iconH);
                    params.setMargins(x, y, iconW, iconH);
                    iv.setLayoutParams(params);


                    if(event.getAction()==1){
                        imageView.setImageResource(imgSrc);
                        iv.setImageResource(imgSrc);

//                      判断是否拖拽到显示区某个图标的附近
                        if(total==1){
                            if(x>=(450-iconW0)*width/1280 && x<=(450+iconW0)*width/1280 && y>=(100-iconH0)*height/480 && y<=(100+iconH0)*height/480){
                                iv_add1.setImageResource(imgSrc);
                                et_add1.setText(bytesToString(bytes));

                                wName1 = bean.getName();
                                imgSrc1 = bean.getImgSrc();
                                imgSrc1f = bean.getImgSrc2();
                                bytes1 = bean.getBytes();
                            }
                        }else if(total==2){
                            if(x>=(275-iconW0)*width/1280 && x<=(275+iconW0)*width/1280 && y>=(100-iconH0)*height/480 && y<=(100+iconH0)*height/480){
                                iv_add1.setImageResource(imgSrc);
                                et_add1.setText(bytesToString(bytes));

                                wName1 = bean.getName();
                                imgSrc1 = bean.getImgSrc();
                                imgSrc1f = bean.getImgSrc2();
                                bytes1 = bean.getBytes();
                            }else if(x>=(625-iconW0)*width/1280 && x<=(625+iconW0)*width/1280 && y>=(100-iconH0)*height/480 && y<=(100+iconH0)*height/480){
                                iv_add2.setImageResource(imgSrc);
                                et_add2.setText(bytesToString(bytes));

                                wName2 = bean.getName();
                                imgSrc2 = bean.getImgSrc();
                                imgSrc2f = bean.getImgSrc2();
                                bytes2 = bean.getBytes();
                            }
                        }else if(total==3){
                            if(x>=(100-iconW0)*width/1280 && x<=(100+iconW0)*width/1280 && y>=(100-iconH0)*height/480 && y<=(100+iconH0)*height/480){
                                iv_add1.setImageResource(imgSrc);
                                et_add1.setText(bytesToString(bytes));

                                wName1 = bean.getName();
                                imgSrc1 = bean.getImgSrc();
                                imgSrc1f = bean.getImgSrc2();
                                bytes1 = bean.getBytes();
                            }else if(x>=(450-iconW0)*width/1280 && x<=(450+iconW0)*width/1280 && y>=(100-iconH0)*height/480 && y<=(100+iconH0)*height/480){
                                iv_add2.setImageResource(imgSrc);
                                et_add2.setText(bytesToString(bytes));

                                wName2 = bean.getName();
                                imgSrc2 = bean.getImgSrc();
                                imgSrc2f = bean.getImgSrc2();
                                bytes2 = bean.getBytes();
                            }else if(x>=(800-iconW0)*width/1280 && x<=(800+iconW0)*width/1280 && y>=(100-iconH0)*height/480 && y<=(100+iconH0)*height/480){
                                iv_add3.setImageResource(imgSrc);
                                et_add3.setText(bytesToString(bytes));

                                wName3 = bean.getName();
                                imgSrc3 = bean.getImgSrc();
                                imgSrc3f = bean.getImgSrc2();
                                bytes3 = bean.getBytes();
                            }
                        }else if(total==4){
                            if(x>=(275-iconW0)*width/1280 && x<=(275+iconW0)*width/1280 && y>=(10-iconH0)*height/480 && y<=(10+iconH0)*height/480){
                                iv_add1.setImageResource(imgSrc);
                                et_add1.setText(bytesToString(bytes));

                                wName1 = bean.getName();
                                imgSrc1 = bean.getImgSrc();
                                imgSrc1f = bean.getImgSrc2();
                                bytes1 = bean.getBytes();
                            }else if(x>=(625-iconW0)*width/1280 && x<=(625+iconW0)*width/1280 && y>=(10-iconH0)*height/480 && y<=(10+iconH0)*height/480){
                                iv_add2.setImageResource(imgSrc);
                                et_add2.setText(bytesToString(bytes));

                                wName2 = bean.getName();
                                imgSrc2 = bean.getImgSrc();
                                imgSrc2f = bean.getImgSrc2();
                                bytes2 = bean.getBytes();
                            }else if(x>=(275-iconW0)*width/1280 && x<=(275+iconW0)*width/1280 && y>=(180-iconH0)*height/480 && y<=(180+iconH0)*height/480){
                                iv_add3.setImageResource(imgSrc);
                                et_add3.setText(bytesToString(bytes));

                                wName3 = bean.getName();
                                imgSrc3 = bean.getImgSrc();
                                imgSrc3f = bean.getImgSrc2();
                                bytes3 = bean.getBytes();
                            }else if(x>=(625-iconW0)*width/1280 && x<=(625+iconW0)*width/1280 && y>=(180-iconH0)*height/480 && y<=(180+iconH0)*height/480){
                                iv_add4.setImageResource(imgSrc);
                                et_add4.setText(bytesToString(bytes));

                                wName4 = bean.getName();
                                imgSrc4 = bean.getImgSrc();
                                imgSrc4f = bean.getImgSrc2();
                                bytes4 = bean.getBytes();
                            }
                        }else if(total==5){
                            if(x>=(275-iconW0)*width/1280 && x<=(275+iconW0)*width/1280 && y>=(10-iconH0)*height/480 && y<=(10+iconH0)*height/480){
                                iv_add1.setImageResource(imgSrc);
                                et_add1.setText(bytesToString(bytes));

                                wName1 = bean.getName();
                                imgSrc1 = bean.getImgSrc();
                                imgSrc1f = bean.getImgSrc2();
                                bytes1 = bean.getBytes();
                            }else if(x>=(625-iconW0)*width/1280 && x<=(625+iconW0)*width/1280 && y>=(10-iconH0)*height/480 && y<=(10+iconH0)*height/480){
                                iv_add2.setImageResource(imgSrc);
                                et_add2.setText(bytesToString(bytes));

                                wName2 = bean.getName();
                                imgSrc2 = bean.getImgSrc();
                                imgSrc2f = bean.getImgSrc2();
                                bytes2 = bean.getBytes();
                            }else if(x>=(100-iconW0)*width/1280 && x<=(100+iconW0)*width/1280 && y>=(180-iconH0)*height/480 && y<=(180+iconH0)*height/480){
                                iv_add3.setImageResource(imgSrc);
                                et_add3.setText(bytesToString(bytes));

                                wName3 = bean.getName();
                                imgSrc3 = bean.getImgSrc();
                                imgSrc3f = bean.getImgSrc2();
                                bytes3 = bean.getBytes();
                            }else if(x>=(450-iconW0)*width/1280 && x<=(450+iconW0)*width/1280 && y>=(180-iconH0)*height/480 && y<=(180+iconH0)*height/480){
                                iv_add4.setImageResource(imgSrc);
                                et_add4.setText(bytesToString(bytes));

                                wName4 = bean.getName();
                                imgSrc4 = bean.getImgSrc();
                                imgSrc4f = bean.getImgSrc2();
                                bytes4 = bean.getBytes();
                            }else if(x>=(800-iconW0)*width/1280 && x<=(800+iconW0)*width/1280 && y>=(180-iconH0)*height/480 && y<=(180+iconH0)*height/480){
                                iv_add5.setImageResource(imgSrc);
                                et_add5.setText(bytesToString(bytes));

                                wName5 = bean.getName();
                                imgSrc5 = bean.getImgSrc();
                                imgSrc5f = bean.getImgSrc2();
                                bytes5 = bean.getBytes();
                            }
                        }else if(total==6){
                            if(x>=(100-iconW0)*width/1280 && x<=(100+iconW0)*width/1280 && y>=(10-iconH0)*height/480 && y<=(10+iconH0)*height/480){
                                iv_add1.setImageResource(imgSrc);
                                et_add1.setText(bytesToString(bytes));

                                wName1 = bean.getName();
                                imgSrc1 = bean.getImgSrc();
                                imgSrc1f = bean.getImgSrc2();
                                bytes1 = bean.getBytes();
                            }else if(x>=(450-iconW0)*width/1280 && x<=(450+iconW0)*width/1280 && y>=(10-iconH0)*height/480 && y<=(10+iconH0)*height/480){
                                iv_add2.setImageResource(imgSrc);
                                et_add2.setText(bytesToString(bytes));

                                wName2 = bean.getName();
                                imgSrc2 = bean.getImgSrc();
                                imgSrc2f = bean.getImgSrc2();
                                bytes2 = bean.getBytes();
                            }else if(x>=(800-iconW0)*width/1280 && x<=(800+iconW0)*width/1280 && y>=(10-iconH0)*height/480 && y<=(10+iconH0)*height/480){
                                iv_add3.setImageResource(imgSrc);
                                et_add3.setText(bytesToString(bytes));

                                wName3 = bean.getName();
                                imgSrc3 = bean.getImgSrc();
                                imgSrc3f = bean.getImgSrc2();
                                bytes3 = bean.getBytes();
                            }else if(x>=(100-iconW0)*width/1280 && x<=(100+iconW0)*width/1280 && y>=(180-iconH0)*height/480 && y<=(180+iconH0)*height/480){
                                iv_add4.setImageResource(imgSrc);
                                et_add4.setText(bytesToString(bytes));

                                wName4 = bean.getName();
                                imgSrc4 = bean.getImgSrc();
                                imgSrc4f = bean.getImgSrc2();
                                bytes4 = bean.getBytes();
                            }else if(x>=(450-iconW0)*width/1280 && x<=(450+iconW0)*width/1280 && y>=(180-iconH0)*height/480 && y<=(180+iconH0)*height/480){
                                iv_add5.setImageResource(imgSrc);
                                et_add5.setText(bytesToString(bytes));

                                wName5 = bean.getName();
                                imgSrc5 = bean.getImgSrc();
                                imgSrc5f = bean.getImgSrc2();
                                bytes5 = bean.getBytes();
                            }else if(x>=(800-iconW0)*width/1280 && x<=(800+iconW0)*width/1280 && y>=(180-iconH0)*height/480 && y<=(180+iconH0)*height/480){
                                iv_add6.setImageResource(imgSrc);
                                et_add6.setText(bytesToString(bytes));

                                wName6 = bean.getName();
                                imgSrc6 = bean.getImgSrc();
                                imgSrc6f = bean.getImgSrc2();
                                bytes6 = bean.getBytes();
                            }
                        }

                        iv.setVisibility(View.GONE);
                    }else{
                        imageView.setImageResource(imgSrcf);
                        iv.setImageResource(imgSrcf);
                    }

                    return true;
                }
            });

            return layout;
        }
    }


    //点系统返回键到设置界面
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        Log.e("onKeyDown===", "==="+keyCode);

        if (keyCode == KeyEvent.KEYCODE_BACK) {

//            IntentUtils.startActivity(activity, MainActivity.class);
            finishMine();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
